package cn.zhoutingrui.pethome.system.dept.controller;

import cn.zhoutingrui.pethome.config.BussnisEptional;
import cn.zhoutingrui.pethome.system.dept.domain.DeptEchart;
import cn.zhoutingrui.pethome.system.dept.service.IDepartmentService;
import cn.zhoutingrui.pethome.system.dept.domain.Department;
import cn.zhoutingrui.pethome.system.dept.query.DepartmentQuery;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门功能",description = "部门的crud")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;


    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation("部门的添加和修该")
    public AjaxResultUtil addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResultUtil.success();
        } catch (BussnisEptional e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }

    //批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResultUtil batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResultUtil.success();
        } catch (BussnisEptional e) {
            throw new RuntimeException(e);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Department get(@PathVariable("id")Long id)
    {
        return departmentService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public AjaxResultUtil list(){
        List<Department> all = departmentService.findAll();
        return AjaxResultUtil.success().setData(all);
    }

    //获取部门树
    @PostMapping("/tree")
    public AjaxResultUtil getTree(){
        List<Department> tree = departmentService.getTree();
        return AjaxResultUtil.success().setData(tree);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResultUtil json(@RequestBody DepartmentQuery query)
    {
        try {
            List<Department> list = departmentService.findQuery(query);
            Long count = departmentService.findByQueryCount(query);
            return AjaxResultUtil.success().setData(list).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }

    @GetMapping("/echarts")
    @ApiOperation("统计各部门下的员工数量")
    public AjaxResultUtil countEmpByDept(){
        List<DeptEchart> deptEcharts = departmentService.countEmpByDept();
        return AjaxResultUtil.success().setData(deptEcharts);
    }
}
